package afcp.alumni.action.base;

import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.validation.Validate;
import afcp.alumni.action.event.LoginAction;
import afcp.alumni.action.event.ManageMemberAction;
import afcp.alumni.action.event.UploadMemberFileAction;
import afcp.alumni.action.page.AdminAction;
import afcp.alumni.action.page.BugListAction;
import afcp.alumni.action.page.HomeAction;
import afcp.alumni.action.page.MemberDetailAction;
import afcp.alumni.action.page.MembersAction;
import afcp.alumni.action.page.MyspaceAction;
import afcp.alumni.service.impl.LoginService;

/**
 * <p>
 * Parent class of all action beans in this project (replacing the servlets).
 * This class receives the requests sent to the URL specified by the annotation
 * {@literal @}{@link UrlBinding}(value="<em>/my/url</em>").
 * </p>
 * <p>
 * A basic implementation would be to set an UrlBinding annotation to the class
 * and then implement the four abstract methods {@link #getUrl()},
 * {@link #view()}, {@link #getTitle()} and {@link #defaultJsp()} to return the
 * content of a JSP File.
 * </p>
 * <p>
 * You may also want to call other methods returning a {@link Resolution} object
 * by putting the method name in the URL as the key of an argument (GET argument
 * for instance) or the "name" attribute of a submit input. This is useful for
 * AJAX calls. This class also implements convenience methods common to the
 * different actions.
 * </p>
 * <p>
 * In the JSP code, this action's getters may be accessed using an Expression
 * Language like, for instance, <code>${actionBean.title}</code> to display the
 * result of the method getTitle() (without any arguments).
 * </p>
 * <p>
 * As a Stripes action, the content of the fields in a Stripes form (in the JSP)
 * is automatically reported in the corresponding action properties. You may use
 * the {@literal @}{@link Validate} annotation on these properties to set
 * validation conditions. If the form fields don't match, the form is not
 * validated and the default page is displayed again with error messages (if set
 * on the JSP using the Stripes error tags).
 * </p>
 * 
 * @author Antoine
 * 
 */
public abstract class PageAction extends BaseAction {

//	private static final Log logger = LogFactory.getLog(PageAction.class);

	// les méthodes get...Url permettent aux jsp d'accéder aux URL de toutes les
	// actions, par exemple pour faire un lien.
	public String getHomeUrl() {
		return HomeAction.URL;
	}

	public String getMembresUrl() {
		return MembersAction.URL;
	}

	public String getAdminUrl() {
		return AdminAction.URL;
	}

	public String getMemberDetailUrl() {
		return MemberDetailAction.URL;
	}

	public String getUploadMemberUrl() {
		return UploadMemberFileAction.URL;
	}

	public String getLoginUrl() {
		return LoginAction.URL;
	}
	
	public String getBugListUrl() {
		return BugListAction.URL;
	}
	
	public String getManageMemberUrl() {
		return ManageMemberAction.URL;
	}
	
	public String getMyspaceUrl() {
		return MyspaceAction.URL;
	}
	
//	private boolean home = false;
//	private boolean membres = false;
//	private boolean admin = false;
//
//	public boolean isHome() {
//		return home;
//	}
//
//	public void setHome() {
//		this.home = true;
//	}
//
//	public boolean isMembres() {
//		return membres;
//	}
//
//	public void setMembres() {
//		this.membres = true;
//	}
//	
//	public boolean isAdmin() {
//		return admin;
//	}
//
//	public void setAdmin() {
//		this.admin = true;
//	}

//	private boolean memberDetail;
//	private boolean uploadMember;
//	private boolean login;
	
//	@Before
//	protected abstract void setPageVisitedBoolean();

	/**
	 * Defines the title which will be used as the page title in the HTML code
	 * returned. This property is used in the template file
	 * WEB-INF/jsp/generic/layout.jsp.
	 * 
	 * @return This HTML page's title
	 */
	public abstract String getTitle();

	/**
	 * Return the path to the default JSP file associated to this action. It
	 * must be an application-relative path like "WEB-INF/jsp/index.jsp".
	 * 
	 * @return The path to the default JSP file.
	 */
	protected abstract String defaultJsp();

	/**
	 * Returns a {@link ForwardResolution} object to the default JSP file (set
	 * by implementing {@link #defaultJsp()}). It means that the content of this
	 * JSP is returned as the answer to the request.<br/>
	 * This is a shorthand for
	 * <code>return new ForwardResolution(this.defaultJsp());</code>.
	 * 
	 * @return A {@link ForwardResolution} object to the default JSP file.
	 */
	protected Resolution toJsp() {
		return new ForwardResolution(this.defaultJsp());
	}

//	private boolean _hasMessage = false;

	/**
	 * Returns a boolean indicating whether an error occurred in the action that
	 * should be displayed on the page.
	 * 
	 * @return true if an error occurred and must be displayed on the page,
	 *         false otherwise.
	 */
	public boolean hasMessage() {
		return message != null && !message.isEmpty();
	}

//	public static final int CRIT_NOTHING = -1;
//	public static final int CRIT_INFO = 1;
//	public static final int CRIT_WARNING = 2;
//	public static final int CRIT_ERROR = 3;
	private Criticity criticity = Criticity.NOTHING;
	
	public enum Criticity {
		NOTHING, INFO, WARNING, ERROR
	}

	public Criticity getCriticity() {
		return criticity;
	}

	private String message = null;

	/**
	 * The error message that should be displayed if an error occurred. You
	 * might want to check {@link #hasMessage()} before dealing with this error
	 * message.
	 * 
	 * @return The error message that should be displayed if an error occurred.
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * Define the error message that should be displayed if an error occurred.
	 * Call this method if an error occurred. This method will also set the
	 * value of {@link #hasMessage()} depending on whether the error message is
	 * empty or not.
	 * 
	 * @param errorMessage
	 *            the error message to display or null/empty string otherwise.
	 */
	public void error(String errorMessage) {
		if (errorMessage != null && !errorMessage.isEmpty()) {
			criticity = Criticity.ERROR;
			this.message = errorMessage;
		} else {
			criticity = Criticity.NOTHING;
			this.message = null;
		}
	}

	public void warning(String warningMessage) {
		if (warningMessage != null && !warningMessage.isEmpty()) {
			criticity = Criticity.WARNING;
			this.message = warningMessage;
		} else {
			criticity = Criticity.NOTHING;
			this.message = null;
		}
	}
	
//	public String getRoleAdmin() {
//		return MemberUtils.ROLE_ADMIN;
//	}

	//TODO vérifier la liste des rôles conservés.
	public static final String VISIBLE = LoginService.ROLE_ADMIN+","+ LoginService.ROLE_VIP + "," + LoginService.ROLE_MEMBER;
	public String getRoleVisible() {
		return VISIBLE;
	}
	private static final String NOT_VISIBLE = "not "+LoginService.ROLE_ADMIN+","+"not "+LoginService.ROLE_VIP + "," + "not "+ LoginService.ROLE_MEMBER;
	public String getRoleNotVisible() {
		return NOT_VISIBLE;
	}
	
	public String getQuery() {
		return context.getQuery();
	}

	
//	@After(stages=LifecycleStage.EventHandling)
//	public void doStuff() {
//		logger.info("registering URL: " + getUrl());
//		context.registerSourcePageURL(getUrl());
//	}

}
